<?php
/**
 * TomatoCMS
 *
 * LICENSE
 *
 * This source file is subject to the GNU GENERAL PUBLIC LICENSE Version 2
 * that is bundled with this package in the file LICENSE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://www.gnu.org/licenses/gpl-2.0.txt
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to license@tomatocms.com so we can send you a copy immediately.
 *
 * @copyright    Copyright (c) 2009-2010 TIG Corporation (http://www.tig.vn)
 * @license        http://www.gnu.org/licenses/gpl-2.0.txt GNU GENERAL PUBLIC LICENSE Version 2
 * @version     $Id: view.phtml 3287 2010-06-04 09:37:56Z huuphuoc $
 */
?>
<?php
$this->headTitle($this->translator('order_list_page_title'));

$this->headLink()
    ->appendStylesheet($this->APP_STATIC_SERVER.'/js/jquery.ui/themes/base/ui.core.css')
    ->appendStylesheet($this->APP_STATIC_SERVER.'/js/jquery.ui/themes/base/ui.resizable.css')
    ->appendStylesheet($this->APP_STATIC_SERVER.'/js/jquery.ui/themes/base/ui.dialog.css')
    ->appendStylesheet($this->APP_STATIC_SERVER.'/js/jquery.ui/themes/base/ui.theme.css')
    ->appendStylesheet($this->APP_STATIC_SERVER . '/js/jquery.ui/themes/base/ui.datepicker.css')
    ;

$this->headScript()
    ->appendFile($this->APP_STATIC_SERVER.'/js/jquery.ui/ui.core.js')
    ->appendFile($this->APP_STATIC_SERVER.'/js/jquery.ui/ui.draggable.js')
    ->appendFile($this->APP_STATIC_SERVER.'/js/jquery.ui/ui.resizable.js')
    ->appendFile($this->APP_STATIC_SERVER.'/js/jquery.ui/ui.dialog.js')
    ->appendFile($this->APP_STATIC_SERVER . '/js/jquery.ui/ui.datepicker.js')
    ;
?>

<div class="grid_12 t_a_ui_breadcrumb">
    <span><a href="<?php echo $this->url(array(), 'order_order_list'); ?>"><?php echo $this->translator('order_list_breadcump'); ?></a></span>
</div>

<!-- Search form -->
<form method="post" action="<?php echo $this->url(array(), 'order_order_list'); ?>" id="searchArticleForm">
<div class="grid_12 t_a_bottom">
    <div class="prefix_1 suffix_1">
        <div class="clearfix t_a_bottom">
            <div class="grid_10 alpha">
                <label><?php echo $this->translator('order_tracking_date_from')?>:</label>
                <input value="<?php echo isset($this->exp['order_created_form_date'])?date('d/m/Y',strtotime($this->exp['order_created_form_date'])):null;?>" type="text" name="order_created_form_date" id="order_created_form_date" style="width: 100px"/>&nbsp;&nbsp;&nbsp;
                <?php echo $this->translator('order_tracking_date_to')?>:
                <input value="<?php echo isset($this->exp['order_created_to_date'])?date('d/m/Y',strtotime($this->exp['order_created_to_date'])):null;?>" type="text" name="order_created_to_date" id="order_created_to_date" style="width: 100px" />
            </div>
        </div>

        <hr />

        <div class="clearfix t_a_bottom">
            <div class="grid_5 alpha">
                <label><?php echo $this->translator('order_tracking_status'); ?>:</label>
                <select name="order_tracking_status" style="width: 150px;">
                    <option value="all">--<?php echo $this->translator('order_selected_all');?>--</option>
                    <?php foreach($this->arrSStatus as $status):?>
                    <option <?php if(@$this->exp['order_tracking_status'] == $status->id):?>selected<?php endif;?> value="<?php echo $status->id;?>"><?php echo $this->translator('order_tracking_status_'.$status->code);?></option>
                    <?php endforeach;?>
                </select>
            </div>
            <div class="grid_5 omega">
                <label><?php echo $this->translator('order_status'); ?>:</label>
                <select name="order_status" style="width:150px;">
                    <option value="all">--<?php echo $this->translator('order_selected_all');?>--</option>
                    <option <?php if(@$this->exp['order_status'] == 'valid'){?> selected="selected"<?php }?> value="valid"><?php echo $this->translator('order_valid'); ?></option>
                    <option <?php if(@$this->exp['order_status'] == 'canceled'){?> selected="selected"<?php }?> value="canceled"><?php echo $this->translator('order_canceled'); ?></option>
                </select>
            </div>
        </div>

        <hr />

        <div class="clearfix t_a_bottom">
            <label>&nbsp;</label>
            <button type="submit"><span><?php echo $this->translator('product_list_search_button'); ?></span></button>
        </div>
    </div>
</div>
</form>
<!--end search form-->

<?php if ($this->numOrder == 0) : ?>
    <div class="grid_12 t_a_bottom">
        <?php echo $this->translator('order_list_not_found'); ?>
    </div>
<?php else : ?>
    <div class="grid_12 t_a_ui_data_list_header">
        <div class="grid_2 alpha"><?php echo $this->translator('order_id'); ?></div>
        <div class="grid_1 alpha"><?php echo $this->translator('order_create_date'); ?></div>
        <div class="grid_1" style="width:70px;"><?php echo $this->translator('order_status'); ?></div>
        <div class="grid_2"><?php echo $this->translator('order_customer'); ?></div>
        <div class="grid_1" style="width:75px;"><?php echo $this->translator('order_total_shipping_cost'); ?></div>
        <div class="grid_1" style="width:70px;"><?php echo $this->translator('order_amount'); ?></div>
        <div class="grid_1" style="width:100px;"><?php echo $this->translator('order_tracking_status'); ?></div>
        <div class="grid_1"><?php echo $this->translator('buyer'); ?></div>
        <div class="grid_1"><?php echo $this->translator('seller'); ?></div>
    </div>

    <div class="grid_12 t_a_bottom" id="orders">
    <?php foreach ($this->order as $row) : ?>
        <?php
            /**
            * To view
            **/
            $t_amount = $row->order_total_amount.$row->order_total_amount_currency;
            $amount = $t_amount;

            /**
            * Customer info
            **/
            $customer = isset($this->arrCustomer[$row->order_id])?$this->arrCustomer[$row->order_id]:null;
            $c_name = '-';
            if($customer){
                $c_name = $customer->full_name?$customer->full_name:$customer->user_name;

                /**
                * Enterprise name
                **/
                $enterprise = isset($this->arrEName[$row->order_id.'_'.$customer->user_id])?$this->arrEName[$row->order_id.'_'.$customer->user_id]:null;
                $e_name = null;
                if($enterprise){
                    $e_name = $enterprise;
                }
            }

            /**
             * Tracking status
            **/
            $t_status = isset($this->arrStatus[$row->order_id])?$this->arrStatus[$row->order_id]:null;

            /**
             * Evaluation
            **/
            $evaluation = $this->arrEvalOrders[$row->order_id];
        ?>
        <div id="tOrderRow_<?php echo $row->order_id; ?>" class="clearfix t_a_ui_data_list_content">
            <div class="grid_2 alpha">
                <?php echo $row->order_invoice; ?>
                <div class="t_a_top t_a_ui_helper_action">
                    <?php if ($this->allow('view')) : ?>
                    <a href="<?php echo $this->url(array('order_id' => $row->order_id), 'order_order_view'); ?>"><?php echo $this->translator('order_list_action_view'); ?></a>
                    <?php endif; ?>
                    <?php if ($this->allow('delete')) : ?>
                    | <a class="tDeleteAction" rel="<?php echo $row->order_id; ?>" href="javascript: void(0)"><?php echo $this->translator('order_list_action_delete'); ?></a>
                    <?php endif; ?>
                </div>
            </div>
            <div class="grid_1 alpha"><?php echo date('d-m-Y',strtotime($row->order_created_date)); ?></div>
            <div class="grid_1" style="width:70px;">
                <?php if ($row->order_status == 'canceled'):?>
                    <font color="Red"><?php echo $this->translator('order_'.$row->order_status);?></font>
                <?php else:?>
                    <font color="Green"><?php echo $this->translator('order_'.$row->order_status);?></font>
                <?php endif;?>
                &nbsp;
            </div>
            <div class="grid_2">
                <b><?php echo $this->translator('order_customer_id');?>:</b> <?php echo $row->customer_id; ?>
                <br/>
                <b><?php echo $this->translator('order_customer_name');?>:</b>
                <?php if($this->allow('info', 'customer', 'customer')):?>
                <a class="t_a_link" href="<?php echo $this->url(array('user_id' => $row->customer_id), 'customer_customer_info');?>"><?php echo $c_name;?> <?php echo ($e_name)?'('.$e_name.')':null;?></a>
                <?php else:?>
                <?php echo $c_name;?> <?php echo ($e_name)?'('.$e_name.')':null;?>
                <?php endif;?>
            </div>
            <div class="grid_1" style="width:75px;"><?php echo $row->order_total_shipping_price; ?></div>
            <div class="grid_1" style="width:70px;"><?php echo $amount;?></div>
            <div class="grid_1" style="width:100px;"><?php echo ($t_status)?$this->translator('order_tracking_status_'.$t_status):'-'?></div>
            <div class="grid_1"><input type="checkbox" <?php if(null != @$evaluation->buyer_user_id):?>checked<?php endif;?> disabled="true" /></div>
            <div class="grid_1"><input type="checkbox" <?php if(null != @$evaluation->seller_user_id):?>checked<?php endif;?> disabled="true" /></div>
        </div>
    <?php endforeach; ?>
    </div>

    <div class="grid_12 t_a_bottom t_a_paginator">
        <?php echo $this->paginator()->slide($this->paginator, $this->paginatorOptions); ?>
    </div>
<?php endif; ?>

<div id="tConfirmBox"></div>

<script type="text/javascript">
$(document).ready(function() {
    /**
    * Create datepicker instances
    */
    $('#order_created_form_date').datepicker({ dateFormat: 'dd/mm/yy' });
    $('#order_created_to_date').datepicker({ dateFormat: 'dd/mm/yy' });

    <?php if ($this->allow('delete')) : ?>
    $('a.tDeleteAction').click(function() {
        var self = this;
        $('#tConfirmBox').html('<?php echo addslashes($this->translator('order_list_action_delete_confirm')); ?>')
            .dialog({
                title: '<?php echo addslashes($this->translator('common_dialog_confirm')); ?>',
                closeOnEscape: false,
                resizable: false,
                modal: true,
                open: function(event, ui) {
                    $('.ui-dialog-titlebar-close').hide();
                },
                buttons: {
                    '<?php echo addslashes($this->translator('common_dialog_confirm_no')); ?>': function() {
                        $(this).dialog('destroy');
                    },
                    '<?php echo addslashes($this->translator('common_dialog_confirm_yes')); ?>': function() {
                        $(this).dialog('destroy');
                        $(self).html('').addClass('t_a_loading');
                        var id = $(self).attr('rel');
                        $.ajaxq('news_article', {
                            type: 'POST',
                            url: "<?php echo $this->url(array(), 'order_order_delete'); ?>",
                            data: { id: id },
                            success: function(response) {
                                if ('RESULT_OK' == response) {
                                    $('#tOrderRow_' + id).fadeOut('slow');
                                }
                            }
                        });
                    }
                }
            });
    });
    <?php endif; ?>
});
</script>
